package com.shichenjia.fileupload.controller;

import com.shichenjia.fileupload.common.BaseResponse;
import com.shichenjia.fileupload.common.ResultUtils;
import com.shichenjia.fileupload.entrity.FileUpload;
import com.shichenjia.fileupload.service.imp.FileUploadServiceImp;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@Tag(name = "图片上传")
public class FileUploadController {
    @Autowired
    FileUploadServiceImp fileUploadService;
    @PostMapping(value = "/file-upload", consumes = "multipart/form-data")
    public BaseResponse<List<FileUpload>> fileUpload(@RequestPart MultipartFile[] files) {
        List<FileUpload> fileUploads = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {
            fileUploads.add(fileUploadService.uploadFile(files[i]));
        }
        return ResultUtils.success(fileUploads);
    }
    @GetMapping("/get-file")
    public ResponseEntity<Resource> getFile(@RequestParam("fileId") String fileId) {
        return fileUploadService.getFile(fileId);
    }
}
